#include<string>
#include<unordered_map>
#include<algorithm>
#include<iostream>
using namespace std;

class Solution {
public:
    int longestPalindromeSubseq(string s) {
        //vector<int>dp0(s.length(), 1);
        //vector<int>dp1(s.length(), 1);
        int* dp0 = new int[s.length()];
        int* dp1 = new int[s.length()];
        for (int i = s.length() - 1; i >= 0; --i) {
            for (int j = i; j < s.length(); ++j) {
                if (i == j)
                    dp0[j] = 1;
                else if (s[i] == s[j])
                    dp0[j] = (j==i+1?0:dp1[j - 1]) + 2;
                else
                    dp0[j] = max(dp1[j], dp0[j - 1]);
            }
            swap(dp0, dp1);
        }
        return dp1[s.length() - 1];
    }
};